/**
 * Copyright (C) 2022-2023 Timber Chain Cloud (TimberCLD). All Rights Reserved.
 *
 * @author: luoweijian
 * @email: 807301075@qq..com
 * @site: https://www.timbercld.com/
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *  limitations under the License.
 */
import http from '@/utils/request';

// #region 类型
type User = {
  id: string;
  token: string;
};

type UserInfo = {
  id: string;
  token: string;
  username: string;
  superAdmin: number;
  superSubSystem: number;
  realName: string;
};
type ErrorValue = {
  error: string;
};
// #endregion

const url = '/timbercld'; // 默认url前缀，可新增其他api配置

export const globalApi = {
  // 登录接口
  login(params: object) {
    return http.post<User>(url + '/login', params);
  },
  // 退出登录
  logout() {
    return http.post<User>(url + '/loginout');
  },
  // 获取菜单
  getMens() {
    return http.get(url + '/system/menu/nav');
  },
  // 管理员信息
  getAdminInfo() {
    return http.get<UserInfo>(url + '/system/user/info');
  },
  // 获取权限
  getPermissions() {
    return http.get(url + '/system/menu/permissions');
  },
  // 修改密码
  password(params: object) {
    return http.put(url + '/system/user/password', params);
  },
};

export const schedulerApi = {
  // #region 定时计划接口
  getSchedulerPageUrl: `${url}/system/scheduler/page`, // 列表
  deleteScheduler: `${url}/system/scheduler`, // 删除
  pauseScheduler(ids: string[]) {
    // 暂停日志
    return http.put(url + '/system/scheduler/pause', ids);
  },
  resumeSchedule(ids: string[]) {
    // 恢复日志
    return http.put(url + '/system/scheduler/resume', ids);
  },
  runScheduler(ids: string[]) {
    // 执行日志
    return http.put(url + '/system/scheduler/run', ids);
  },
  getErrorInfo(id: string) {
    // 获取失败信息
    return http.get<ErrorValue>(url + `/system/schedulerlog/${id}`);
  },
  insertScheduler(data: object) {
    // 新增日志
    return http.post(url + '/system/scheduler', data);
  },
  getSchedulerInfo(id: string) {
    // 获取日志信息
    return http.get(url + `/system/scheduler/${id}`);
  },
  updateScheduler(data: object) {
    // 修改日志
    return http.put(url + '/system/scheduler', data);
  },
  // #endregion
};

export const subApi = {
  // #region 子系统用户接口
  getSubListUrl: `${url}/sub/system/page`, // 列表
  deleteSub: `${url}/sub/system`, // 删除
  exportSub: `${url}/sub/system/export`, // 导出
  getSubList() {
    return http.get(url + '/sub/system/role/list');
  },
  insertSub(data: object) {
    // 新增参数
    return http.post(url + '/sub/system', data);
  },
  getSubInfo(id: string) {
    // 获取参数信息
    return http.get(url + `/sub/system/${id}`);
  },
  updateSub(data: object) {
    // 修改参数
    return http.put(url + '/sub/system', data);
  },
  // #endregion

  // #region 子系统角色接口
  getSubRoleListUrl: `${url}/sub/system/role/page`, // 列表
  deleteSubRole: `${url}/sub/system/role`, // 删除
  insertSubRole(data: object) {
    // 新增参数
    return http.post(url + '/sub/system/role', data);
  },
  getSubRoleInfo(id: string) {
    // 获取参数信息
    return http.get(url + `/sub/system/role/${id}`);
  },
  updateSubRole(data: object) {
    // 修改参数
    return http.put(url + '/sub/system/role', data);
  },

  // #endregion
};
